package com.tanhua.manage.listener;

import com.tanhua.commons.templates.HuaWeiUGCTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RocketMQMessageListener(topic = "tanhua-publish", consumerGroup = "tanhua-publish-consumer")
public class PublishMessageListener implements RocketMQListener<String> {

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Reference
    private PublishApi publishApi;

    @Override
    /**
     * message 动态id
     */
    public void onMessage(String message) {
        // 1.调用服务提供者api查询动态内容
        Publish publish = publishApi.findById(message);

        // 2.检测
        if (publish != null) {

            Integer state = publish.getState();

            // 2.1 检测文字内容
            boolean textContentCheck = huaWeiUGCTemplate.textContentCheck(publish.getTextContent());

            if (textContentCheck) {
                // 2.2 检测图片内容
                List<String> medias = publish.getMedias();
                boolean imageContentCheck = huaWeiUGCTemplate.imageContentCheck(medias.toArray(new String[]{}));
                if (imageContentCheck) {
                    state = 1;
                } else {
                    state = 2;
                }

            } else {
                state = 2;
            }

            // 3.更新动态的状态
            publishApi.updateState(message, state);
        }

    }
}
